package com.csjbot.blackgaga.feature.settings.charge_setting;

import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.csjbot.blackgaga.R;
import com.csjbot.blackgaga.base.BaseFullScreenActivity;
import com.csjbot.blackgaga.base.BasePresenter;
import com.csjbot.blackgaga.util.BlackgagaLogger;
import com.csjbot.blackgaga.widget.NewRetailDialog;
import com.csjbot.coshandler.client_req.ReqFactory;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : chenqi.
 * e_mail : 1650699704@163.com.
 * create_time : 2017/10/20.
 * 充电设置页面
 */

public class ChargeSettingActivity extends BaseFullScreenActivity implements ChargeSettingContract.view {
    @BindView(R.id.checkbox)
    SwitchCompat checkbox;
    @BindView(R.id.save)
    Button save;
    @BindView(R.id.charge)
    Button charge;
    @BindView(R.id.tv_electric)
    TextView mTvElectric;
    @BindView(R.id.iv_bottom)
    ImageView mIvBottom;
    @BindView(R.id.iv_top)
    ImageView mIvTop;
    private ChargeSettingContract.presenter presenter;

    private static int SAVE = 1;
    private static int SAVEANDGO = 2;

    @Override
    protected BasePresenter getPresenter() {
        return presenter;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_charge_setting;
    }


    @Override
    public void init() {
        super.init();
        getTitleView().setSettingsVisibility(View.GONE);
        presenter = new ChargeSettingPresenter(this);
        presenter.initView(this);
        getTitleView().setSettingsPageBackVisibility(View.VISIBLE);
        checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                presenter.saveCheckState(true);
            } else {
                presenter.saveCheckState(false);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        BlackgagaLogger.debug("chenqi 是否充电" + presenter.getChargestate());
        checkbox.setChecked(presenter.getChargestate());
        mTvElectric.setText(presenter.getChargeInfor());
    }

    @Override
    public boolean isOpenTitle() {
        return true;
    }

    @OnClick({R.id.save, R.id.charge})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.save:
                presenter.saveChargeInf(mTvElectric.getText().toString().trim());
                presenter.save(SAVE);
                break;
            case R.id.charge:
                presenter.saveChargeInf(mTvElectric.getText().toString().trim());
                presenter.save(SAVEANDGO);
                if (presenter.getChargestate()) {
                    ReqFactory.getChassisReqInstance().goHome();//回家
                }
                break;
        }
    }

    @Override
    public void saveShowDialog(int i) {
        if (i == SAVE) {
            showHintDialog(getString(R.string.charge_save));
        }
    }

    @Override
    public void showCodeMax() {
        Toast.makeText(ChargeSettingActivity.this,  getString(R.string.charge_too_much),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showCodeMin() {
        Toast.makeText(ChargeSettingActivity.this,  getString(R.string.charge_too_ittle),Toast.LENGTH_SHORT).show();
    }

    private void showHintDialog(String msg) {
        showNewRetailDialog(getString(R.string.charge_administration), msg, new NewRetailDialog.OnDialogClickListener() {
            @Override
            public void yes() {
                dismissNewRetailDialog();
            }

            @Override
            public void no() {
                dismissNewRetailDialog();
            }
        });
    }

    @OnClick({R.id.iv_bottom, R.id.iv_top})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_bottom:
                mTvElectric.setText(presenter.addCount(-10)+"%");
                break;
            case R.id.iv_top:
                mTvElectric.setText(presenter.addCount(10)+"%");
                break;
        }
    }
}
